package com.tpo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class weather {
	
	public String ciudad = "Buenos+Aires";
	public String lang = "es";
	public String temperatura = "";
	public String condicion = "";
	public String humedad = "";
	
	public void getXML(){
		
		String url = "http://www.google.com/ig/api?weather="+this.ciudad+"&hl="+this.lang+"&oe=utf-8";
		

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		
		try {
			db = dbf.newDocumentBuilder();
			try {
				InputStream frm = new URL(url).openStream();
				//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(frm, Charset.forName("Latin1")));

				Document doc = db.parse(frm);

				doc.getDocumentElement ().normalize ();
				
				//return doc.getDocumentElement().getNodeName();
				
		        NodeList tmp_c = doc.getElementsByTagName("temp_c");
		        Element tmp_cl = (Element) tmp_c.item(0);
		        String tmp_c_s = tmp_cl.getAttribute("data");
		        this.temperatura = tmp_c_s;
		        
		        NodeList condition = doc.getElementsByTagName("condition");
		        Element condition_l = (Element) condition.item(0);
		        String condition_c_s = condition_l.getAttribute("data");
		        this.condicion = condition_c_s;
		        
		        NodeList Humidity = doc.getElementsByTagName("humidity");
		        Element Humidity_l = (Element) Humidity.item(0);
		        String Humidity_c_s = Humidity_l.getAttribute("data");
		        this.humedad = Humidity_c_s;
				
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
	
	public void setConf(String Ciudad,String Lang){
		this.ciudad = Ciudad;
		this.lang = Lang;
	}
}
